//
//  NetworkRequest.m
//  XingXingLao
//
//  Created by 劳景醒 on 16/2/22.
//  Copyright © 2016年 laojingxing. All rights reserved.
//

#import "NetworkRequest.h"

@implementation NetworkRequest
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.XMLDataArr = [NSMutableArray array];
    }
    return self;
}


// get请求
+ (void)getRequestWithUrl:(NSString *)urlString params:(NSDictionary *)params completion:(void (^)(id result))completionBlock failure:(void (^)(void))failureBlock
{
    // 1.创建请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    // 2、数据反序列化 （因为在进行请求服务器之前会对参数进行一次参数序列化）
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:urlString parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        // 下载进度
        NSLog(@"downloadProgress:%@", downloadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        // 请求成功
        // 解析数据
        
        NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        completionBlock(obj);
        NSLog(@"请求成功");
        

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        // 请求失败
        failureBlock();
        NSLog(@"网络连接失败");
        NSLog(@"%@", task.response.URL);
    }];
}


// post ,json
+ (void)postRequestWithUrl:(NSString *)urlString params:(NSDictionary *)params completion:(void (^)(id))completionBlock failure:(void (^)(void))failureBlock
{


    // 1、创建请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2、数据反序列化 （因为在进行请求服务器之前会对参数进行一次参数序列化）
//    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager.requestSerializer setValue:@"laojingxinghead" forHTTPHeaderField:@"name"];
    // 3、开始请求
    [manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // 上传进度
//        NSLog(@"$%@", uploadProgress.userInfo);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
//        NSLog(@"结果%@", responseObsject);
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"string%@", string);
        // 解析数据
          NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        completionBlock(obj);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络连接失败");
        
        NSLog(@"%@", task.response.URL);
    }];
}

// 没有提标请求失败的，专用于多次调用，防止没网络情况的时候，多次调用
+ (void)postRequestWithUrlNoMessage:(NSString *)urlString params:(NSDictionary *)params completion:(void (^)(id))completionBlock failure:(void (^)(void))failureBlock
{
    
    
    // 1、创建请求管理者
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    // 2、数据反序列化 （因为在进行请求服务器之前会对参数进行一次参数序列化）
    //    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    // 3、开始请求
    [manager POST:urlString parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        // 上传进度
        //        NSLog(@"$%@", uploadProgress.userInfo);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //        NSLog(@"结果%@", responseObsject);
        
        // 解析数据
        NSDictionary *obj = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        completionBlock(obj);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络连接失败");
        NSLog(@"%@", task.response.URL);
    }];
}



@end
